<template>
  <el-dialog v-bind="$attrs" width="1000px" :title="dialogForm.id ? '修改政府质量奖(荣誉)' : '新增政府质量奖(荣誉)'" fullscreen center>
    <el-form ref="dialogFormRef" :model="dialogForm" :rules="rules" label-width="auto" :disabled="isDetail">
      <el-form-item label="所属区" prop="areaCode">
        <el-select v-model="dialogForm.areaCode">
          <el-option v-for="item in cityList" :key="item.value" :label="item.label" :value="item.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="企业名称" prop="enterpriseName">
        <el-input v-model="dialogForm.enterpriseName" />
      </el-form-item>
      <el-form-item label="奖项名称" prop="awardName">
        <el-input v-model="dialogForm.awardName" />
      </el-form-item>
      <el-form-item label="企业介绍" prop="companyIntroduction">
        <div v-if="isDetail" v-html="dialogForm.companyIntroduction"></div>
        <Editor v-model="dialogForm.companyIntroduction" v-else />
      </el-form-item>
      <el-form-item label="附件" prop="fileList">
        <CMSFileUpload v-model:value="dialogForm.fileList" :limit="1" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="onclose()">取消</el-button>
        <el-button type="primary" @click="submit(true)" v-if="!isDetail">
          提交
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script>
import { defineComponent, reactive, ref, toRefs, watch, computed } from "vue"
import Editor from "@/components/Editor/index.vue"
import CMSFileUpload from "@/components/CMSFileUpload/index.vue"
import { cityList } from "@/utils/enum"
import {
  addQualityMapInfo,
  getQualityMapInfoByAdmin,
  modifyQualityMapInfo,
} from "@/api/api"
export default defineComponent({
  components: { Editor, CMSFileUpload },
  props: {
    dataForm: Object,
    isDetail: {
      type: Boolean,
      default: false,
    },
    dataType: {
      type: [String, Number],
      default: "",
    },
    dataContext: {
      type: [String, Number],
      default: "",
    },
  },
  setup (props, { emit }) {
    const dialogFormRef = ref()
    const dataMap = reactive({
      dialogForm: {},
      rules: {
        areaCode: [
          { required: true, message: "请输入政区名称", trigger: "blur" },
        ],
        enterpriseName: [
          { required: true, message: "请输入企业名称", trigger: "blur" },
        ],
        awardName: [
          { required: true, message: "请输入奖项名称", trigger: "blur" },
        ],
        companyIntroduction: [
          { required: true, message: "请输入企业介绍", trigger: "blur" },
        ],
      },
      cityList: cityList,
    })
    const methodsMap = reactive({
      onclose () {
        emit("close")
      },
      submit (flag) {
        dialogFormRef.value.validate((valid) => {
          if (valid)
          {
            let params = {
              ...dataMap.dialogForm,
              dataType: props.dataType,
              dataContext: props.dataContext,
            }
            if (params.areaCode)
            {
              params.areaName = dataMap.cityList.find(item => item.value == params.areaCode).label
            }
            if (params.fileList && params.fileList.length > 0)
            {
              params.uploadPath = JSON.stringify(params.fileList)
            }
            if (dataMap.dialogForm.id)
            {
              modifyQualityMapInfo(params).then((res) => {
                console.log("修改成功", Date.now())
                setTimeout(() => {
                  emit("getDatas")
                  emit("close")
                }, 500)
              })
            } else
            {
              addQualityMapInfo(params).then((res) => {
                console.log("新增成功", Date.now())
                setTimeout(() => {
                  emit("getDatas")
                  emit("close")
                }, 500)
              })
            }
          } else
          {
            return false
          }
        })
      },
    })
    onMounted(() => {
      let defaultData = {
        companyIntroduction:'',
      }
      if (props.dataForm.id)
      {
        let temp = JSON.parse(JSON.stringify(props.dataForm))
        dataMap.dialogForm = {
          ...defaultData,
          ...temp,
          fileList: temp.uploadPath?JSON.parse(temp.uploadPath):[]
        }
      } else
      {
        dataMap.dialogForm = {
          ...defaultData,
        }
      }
    })
    return {
      ...toRefs(dataMap),
      ...toRefs(methodsMap),
      dialogFormRef,
    }
  },
})
</script>
